package com.chezai.client.dao.manager;

import android.support.annotation.NonNull;

import com.chezai.client.bean.ClassRecord;
import com.chezai.client.dao.ClassRecordDao;
import com.chezai.client.dao.DaoManager;
import com.chezai.client.global.Const;
import com.chezai.client.utils.DateUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * 主要：科一 科四
 * 辅助：科二科三
 * 课程数据操作管理类
 * 课程记录与用户信息关联
 * Created by likunyang on 2017/8/29.
 */

public class ClassRecordDaoManager {
    private static ClassRecordDaoManager newInstance;
    private String TAG = this.getClass().getSimpleName();
    private ClassRecordDao dao;

    private ClassRecordDaoManager() {
        dao = DaoManager.getInstance().getDaoSession().getClassRecordDao();
    }

    public static ClassRecordDaoManager getInstance() {
        if (newInstance == null) {
            newInstance = new ClassRecordDaoManager();
        }
        return newInstance;
    }

    public ClassRecordDao getClassRecordDao() {
        return dao;
    }

    public ClassRecord findClassRecord(long _id) {
        ClassRecord classRecord = dao.load(_id);
        return classRecord;
    }

    public void testInsert() {
        int i = 0;
        while (i <= 100) {
            ClassRecord classRecord = new ClassRecord();
            classRecord.setCourseNum("test");
            classRecord.setCreateTime(i%3 ==0 ?"2018/04/13":i%3==1?"2017/04/13":"2018/04/20");
            classRecord.setSid(1L);
            classRecord.setMileage(10000);
            classRecord.setClassId(1l);
            classRecord.setSignInTime(1523588773000l);
            classRecord.setSignOutTime(1523592373000l);
            classRecord.setSignStatus(ClassRecord.SIGN_OUT);
            classRecord.setTrainPart(2);
            classRecord.setTrainSubject("test");
            classRecord.setStudyValidLong(12340);
            long j = dao.insert(classRecord);
            Logger.t("Insert").e("插入一条Classrecord " + j);
            i++;
        }
    }

    public void deleteTimeOutData() {
        try {
            String current = DateUtil.getPastDate(Const.DATA_TIME_OUT_DAY, DateUtil.sf18);
            List<ClassRecord> list = dao.queryBuilder().where(ClassRecordDao.Properties.CreateTime.le(current)).list();
            dao.deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 更新学员签到签退状态
     * 和签到签退时间
     *
     * @param classId  课程Id
     * @param signType 签到类型 ClassRecord .SIGN_IN / .SIGN_OUT
     */
    public void updateClassRecordSignStatus(Long classId, int signType) {
        ClassRecord tempClassRecord = dao.loadByRowId(classId);
        if (tempClassRecord != null) {
            if (signType != tempClassRecord.getSignStatus()) {
                if (signType == ClassRecord.SIGN_IN) {
                    tempClassRecord.setSignInTime(System.currentTimeMillis());
                } else if (signType == ClassRecord.SIGN_OUT) {
                    tempClassRecord.setSignOutTime(System.currentTimeMillis());
                }
                tempClassRecord.setSignStatus(signType);
                dao.update(tempClassRecord);
            }
        }
    }

    public long findLastClassId() {
        List<ClassRecord> list = dao.queryBuilder()
                .where(ClassRecordDao.Properties.CreateTime.eq(DateUtil.timestampStr(DateUtil.sf18)))
                .orderDesc(ClassRecordDao.Properties.ClassId).limit(1).list();
        if (list != null && list.size() > 0) {
            return list.get(0).getClassId();
        }
        return 0;
    }

    /**
     * 插入新的课程记录
     *
     * @param classRecord
     * @return
     */
    public Long insertClassRecord(@NonNull ClassRecord classRecord) {
        if (classRecord != null) {
            classRecord.setCreateTime(getCreateTime());
            classRecord.setSignInTime(System.currentTimeMillis());
            return dao.insert(classRecord);
        }
        return null;
    }


    /**
     * 查找当天所有未签退的记录
     *
     * @return
     */
    public List<ClassRecord> findAllUnSignOut() {
        List<ClassRecord> list = dao.queryBuilder()
                .where(ClassRecordDao.Properties.SignStatus.eq(ClassRecord.SIGN_IN)
                        , ClassRecordDao.Properties.CreateTime.eq(getCreateTime())).list();
        return list;
    }


    private String getCreateTime() {
        return DateUtil.timestampStr("yyyy/MM/dd");
    }

    /**
     * 查找当天是否有未结束的训练
     *
     * @return
     */
    public List<ClassRecord> findUnFinishedTwoOrThreeTrain() {
        List<ClassRecord> list = dao.queryBuilder()
                .where(ClassRecordDao.Properties.SignOutTime.isNull(), ClassRecordDao.Properties.TrainPart.in(Const.TrainPart.TWO, Const.TrainPart.THREE))
                .limit(1)
                .list();
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    public void update(ClassRecord classRecord) {
        dao.update(classRecord);
    }
}
